﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using omsData;
namespace myomscl_web.Admin.UserControls
{
    public partial class ucMenuUser : System.Web.UI.UserControl
    {
        public event OnTreeClick TreeClick;

        protected void tvUserTypes_SelectedNodeChanged(object sender, EventArgs e)
        {
            var selectedNode = this.tvUserTypes.SelectedNode;
            if (selectedNode != null || selectedNode.Parent == null)
            {
                if (string.IsNullOrEmpty(selectedNode.Value))
                {
                    TreeClick("root", null);
                }
                else
                {
                    var parameters = selectedNode.Value.Split(new char[] { ',' });
                    TreeClick(parameters[0], parameters[1]);
                }
            }
        }

        public void LoadTree(string type, string value)
        {
            this.tvUserTypes.Nodes.Clear();

            using (OmsModelContainer context = new OmsModelContainer())
            {
                TreeNode root = new TreeNode("User Type");
                root.Value = string.Empty;
                if (type == "root")
                {
                    //root.Selected = true;
                }

                var userTypes = context.UserTypes.Include("Organisations").ToList();
                foreach (var ut in userTypes)
                {
                    var utNode = new TreeNode(ut.Name);
                    utNode.Value = string.Format("ut,{0}", ut.Id);

                    if (type != null && type == "ut" && value == ut.Name)
                    {
                        //utNode.Selected = true;
                    }

                    root.ChildNodes.Add(utNode);

                    foreach (var org in ut.Organisations)
                    {
                        var orgNode = new TreeNode(org.Name);
                        orgNode.Value = string.Format("org,{0}", org.Id);
                        if (type != null && type == "org" && value == org.Name)
                        {
                            //orgNode.Selected = true;
                        }

                        utNode.ChildNodes.Add(orgNode);
                        org.Users.Load();

                        foreach (var us in org.Users)
                        {
                            string name = string.Format("{0} {1}", us.FirstName, us.LastName);
                            var usNode = new TreeNode(name);
                            usNode.Value = string.Format("us,{0}", us.Id);
                            if (type != null && type == "us" && value == name)
                            {
                                //usNode.Selected = true;
                            }

                            usNode.ChildNodes.Add(usNode);
                        }
                    }
                }

                tvUserTypes.Nodes.Add(root);

                if (type == "root" || string.IsNullOrEmpty(type))
                    this.tvUserTypes.CollapseAll();
                else
                {
                    if (type == "ut")
                        this.tvUserTypes.ExpandAll();
                }
            }
        }
    }

    public delegate void OnTreeClick(string type, string id);
}